import {Component} from '@angular/core';
import {
    AlertController, App, LoadingController, ModalController, NavController, Platform, ToastController,
    UrlSerializer
} from 'ionic-angular';
import {ResetPasswordPage} from "./reset-password/reset-password";
import {SingleList} from "../../common/SingleList/singleList";
import {BudgetUnitPage} from "./budget-unit/budget-unit";
import {AboutUsPage} from "./about-us/about-us";
import {BudgetYearPage} from "./yearBudget/budget-year";
import {LoginPage} from "../login/login";
import {UserManager} from "../../framework/manager/UserManager";
import {SystemService} from "../service/SystemService";
import {BasePage} from "../BasePage";
import {Log} from "../../framework/utils/Log";
import {AppVersion} from '@ionic-native/app-version';
import {SLLocalStorageUtils} from "../../framework/utils/SLLocalStorageUtils";


@Component({
    selector: 'page-my',
    templateUrl: 'my.html',
    providers: [SystemService]
})
export class MyPage extends BasePage{

    public unitLabel: string;
    public yearLabel: string;
    public userName: string;
    public unitName: string;
    public branchName: string;
    public appVersion:any;
    private dataList: any;
    constructor(public navCtrl: NavController, public modalCtrl: ModalController, public app: App,
                public sys: SystemService,
                public platform: Platform,
                public version:AppVersion,
                public loadingCtrl: LoadingController,
                public toastCtrl: ToastController,
                public alertCtrl: AlertController) {
        super(platform, loadingCtrl, toastCtrl, alertCtrl);

    }
    ionViewDidLoad() {
        this.userName = UserManager.getInstance().user.userName;
        this.unitName = UserManager.getInstance().user.unitName;
        this.branchName = UserManager.getInstance().user.branchName;
        this.getAppVersionInfo();
        this.yearLabel =UserManager.getInstance().user.year;
        Log.w('MyPage',this.unitName);
        this.unitLabel=this.unitName;
    }
    resetPassword() {
        this.navCtrl.push(ResetPasswordPage)
    }
    /**
     * 获取AppVersion信息，异步获取
     * @returns {Promise<void>}
     */
    async getAppVersionInfo() {
        this.appVersion = await this.version.getVersionNumber();
    }

    budgetYearHander() {
        let profileModal = this.modalCtrl.create(BudgetYearPage);
        profileModal.onDidDismiss((item) => {
            if (item) {
                this.yearLabel = item.value;
            }
        });
        profileModal.present();
    }

    budgetUnitHander() {
        let profileModal = this.modalCtrl.create(BudgetUnitPage);
        profileModal.onDidDismiss((item) => {
            if (item) {
                this.unitLabel = item.value;
                this.unitName =  this.unitLabel;

            }
        });
        profileModal.present();
    }

    aboutUsHandler() {
        this.navCtrl.push(AboutUsPage)
    }

    async logout() {
        const alert = this.alertCtrl.create({
           title:"提示",
           message:"确定退出内控系统吗?",
           buttons:[{
                text:"取消",
                role:"cancel"
           },{
                text:"确定",
                handler:() => {
                    this.app.getRootNav().setRoot(LoginPage);
                    SLLocalStorageUtils.getInstance().destroy();
                }
           }
        ]
        });
        await alert.present();

        //UserManager.getInstance().user = null;
    }
    getYearList(){
        this.sys.getYearsBudget().subscribe((res:any)=>{
            if(res.msgCode=='1' && res.list.length>0){
                this.dataList =res.list;
                this.yearLabel = res.list[0].text;
                UserManager.getInstance().user.year = this.yearLabel;
            }

        },error=>{
        })
    }

}
